All files / Bandstand/src/app/api/songs/[id]/versions/[n]/restore route.ts

0% Statements 0/30
0% Branches 0/1
0% Functions 0/1
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                                                             
export const dynamic = 'force-dynamic'

import {requireAdmin} from '@/lib/guard'
import {ConflictError, restoreVersion} from '@/lib/songs'
import {route} from '@/lib/route'

// Restoring writes a NEW version with the old content, so nothing is lost;
// it is still admin-only because it undoes someone else's work.
export const POST = route(
  async (_req: Request, {params}: {params: {id: string; n: string}}) => {
    const {user, band} = await requireAdmin()
    const number = Number(params.n)
    if (!Number.isInteger(number) || number < 1)
      return new Response('Bad Request', {status: 400})
    try {
      const v = await restoreVersion({
        bandId: band.id,
        songId: params.id,
        number,
        userId: user.id,
      })
      if (!v) return new Response('Not Found', {status: 404})
      return Response.json({number: v.number}, {status: 201})
    } catch (e) {
      if (e instanceof ConflictError)
        return Response.json({error: e.message}, {status: 409})
      throw e
    }
  },
)